using NUnit.Framework;

namespace Solver
{
	/// <summary>
	/// Find the largest palindrome made from the product of two 3-digit numbers.
	/// </summary>
	public class Problem004 : SelfChecker, IProblem
	{
		public int Id
		{
			get { return 4; }
		}

		public string Solve()
		{
			return Solve(999).ToString();
		}

		[Test]
		public override void Check()
		{
			Assert.AreEqual(9, Solve(9));
			Assert.AreEqual(9009, Solve(99));
			Assert.AreEqual(906609, Solve(999));
		}

		private static int Solve(int maxFactor)
		{
			int maxPal = 0;
			for (int p = maxFactor; p > 0; --p)
				for (int q = p; q > 0; --q)
				{
					int n = p * q;
					if (n <= maxPal)
						break;
					if (Palindrome.IsPalindrome10(n) && n > maxPal)
						maxPal = n;
				}
			return maxPal;
		}
	}
}